'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 联盟捐增清CD
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var moment = require('moment');
var Code = require('../../../../consts/code');
var unionTechConf = require('../../../../config/unionTechConf.js');
var unionGroupModel = require('../../../../models/unionGroupModel');
var unionPositionType = require('../../../../consts/unionPositionType');
var PlayerManager = require('../../../../libs/playerManager');
var buildService = require('../../../../services/buildService.js');
var roleService = require('../../../../services/roleService.js');
var moduleType = require('../../../../consts/moduleType.js');
var operationType = require('../../../../consts/operationType.js');

/**
 * 联盟捐增清CD
 */

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var player_manager = new PlayerManager(role_id);
    var union_group_model;
    var union_id;
    var donated_list = [];
    var donated_time = 0;
    var sub_diamond = 0;
    var role_model;

    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                ['role', 'unionTask', 'accrued'],
                    cb
                );
            },
            function (cb) {
                role_model = player_manager.role;
                union_id = role_model.getUnionId();
                if (!union_id) {
                    return next(null, {
                        code: Code.UNION_IS_NOT_JOIN
                    });
                }
                unionGroupModel.getById(union_id, cb);
            },
            function (model, cb) {
                union_group_model = model;
                var union_task_model = player_manager.unionTask;

                // 扣除钻石
                // todo: 未配置
                var time = union_task_model.donated_time - moment().unix();
                var vip_lv = role_model.vip;
                sub_diamond = buildService.getDirectDiamond(time, vip_lv);
                if (sub_diamond >= 1) {
                    if (!roleService.subDiamond(player_manager, moduleType.MDL_UNION, operationType.OPR_UNION_CREATE, sub_diamond)) {
                        return next(null, {
                            code: Code.USER_DIAMOND_NOT_ENOUGH
                        });
                    }

                    // 重置捐赠时间
                    union_task_model.restDonatedTime();
                    donated_time = union_task_model.donated_time;
                }
                cb();
            },
            function (cb) {
                player_manager.save(cb);
            },
            function (cb) {
                if (!!union_group_model) {
                    union_group_model.save(cb);
                } else {
                    cb();
                }
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    donated_time: donated_time,
                    sub_diamond: sub_diamond
                }
            });
        }
    );
};
